class Solution {
public:
    string longestCommonPrefix(vector<string>& strs) {
        int n = strs.size();
        string res; 
        if (n == 0) return res;
        if (n == 1) return strs[0];    
        for (int i = 0; i < strs[0].size(); ++i) {
            res += strs[0][i];
            for (auto& s: strs) {
                if (s.substr(0, i+1) != res) {
                    res.erase(res.size()-1);
                    return res;
                }
            }
        }
        return res;
    }
};